package com.exampletest.controller;

import com.exampletest.model.User;
import com.exampletest.service.DynamicService;
import com.exampletest.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private UserService userService;

    @Autowired
    private DynamicService dynamicService;

    /**
     * 管理员获取动态列表（支持分页、筛选）
     */
    @GetMapping("/dynamics")
    public Map<String, Object> getAdminDynamicList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(defaultValue = "") String keyword,
            @RequestParam(defaultValue = "") String author,
            @RequestParam(required = false) Integer status) {

        Map<String, Object> result = new HashMap<>();
        try {
            // 计算偏移量
            int offset = (page - 1) * pageSize;
            // 获取分页数据
            Map<String, Object> data = dynamicService.getAdminDynamicList(
                    keyword, author, status, offset, pageSize);
            result.put("code", 200);
            result.put("data", data);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "获取列表失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 审核动态
     */
    @PostMapping("/audit")
    public Map<String, Object> auditDynamic(
            @RequestParam Long dynamicId,
            @RequestParam Integer newStatus,
            @RequestParam(defaultValue = "") String reason) {

        Map<String, Object> result = new HashMap<>();
        try {
            // 调用服务层审核方法
            boolean success = dynamicService.auditDynamic(dynamicId, newStatus, reason);
            if (success) {
                result.put("code", 200);
                result.put("message", "审核成功");
            } else {
                result.put("code", 500);
                result.put("message", "审核失败，动态不存在或状态异常");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "审核失败：" + e.getMessage());
        }
        return result;
    }

    @GetMapping("/dashboard")
    public String dashboard(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        if (user == null || !user.getIsSuperAdmin()) {
            return "redirect:/user/login";
        }
        return "admin/dashboard";
    }

    // 用户管理
    @GetMapping("/list")
    public String listUsers(Model model) {
        List<User> users = userService.getAllUsers();
        model.addAttribute("users", users);
        return "user/list";
    }


    // 其他管理功能...
}